package net.huashitong.synthetical.newSupervision.controller;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.JqGrid;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.sys.service.DeptService;
import com.sirdc.modules.sys.service.SysDeptService;
import com.sirdc.modules.sys.service.SysLoginService;
import com.sirdc.modules.sys.service.SysUserService;
import com.sirdc.modules.sys.util.SysUserUtils;
import com.sirdc.modules.sys.util.UserUtils;
import net.huashitong.synthetical.newSupervision.entity.*;
import net.huashitong.synthetical.newSupervision.filter.TaskNewFilter;
import net.huashitong.synthetical.newSupervision.filter.TaskSendFilter;
import net.huashitong.synthetical.newSupervision.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: ling
 * Date: 2019/7/30
 * Time: 21:04
 * To change this template use File | Settings | File Templates.
 * Description:县领导任务控制层
 */

@Controller
@RequestMapping(value = "/taskLeader")
public class TaskLeaderController extends JsonBaseController {
    @Autowired
    private TaskNewService taskNewService;
    @Autowired
    private TaskSendService taskSendService;
    @Autowired
    private TaskLeaderService taskLeaderService;
    @Autowired
    private TaskLogService taskLogService;
    @Autowired
    private TaskMessageService taskMessageService;
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysLoginService sysLoginService;

    @Autowired
    private DeptService deptService;

    @Override
    protected String getView(String s) {
        return "/modules/project/taskLeader/" + s;
    }

    @RequestMapping("/filter")
    public String filter() {
        return getView("filter");
    }

    @RequestMapping(method = RequestMethod.GET)
    public String initPage() {
        return getView("query");
    }

    /**
     * 县领导任务列表
     *
     * @param taskNewFilter
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST)
    public JqGrid taskLeaderList(TaskNewFilter taskNewFilter) {
        String userId = SysUserUtils.getUserId();
        taskNewFilter.setUserId(userId);
        List<TaskNew> list = taskSendService.queryFilter(taskNewFilter);
        return coverJqGrid(taskNewFilter, list);
    }


    @RequestMapping(value = "/endorseList",method = RequestMethod.GET)
    public String endorseListPage() {
        return getView("queryEndorse");
    }

    @RequestMapping("/endorseList/filter")
    public String endorseFilter() {
        return getView("filter");
    }
    /**
     * 县领导批转任务列表
     *
     * @param taskNewFilter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/endorseList",method = RequestMethod.POST)
    public JqGrid taskEndorse(TaskNewFilter taskNewFilter) {
        taskNewFilter.setLeaderId(SysUserUtils.getUserId());
        List<TaskNew> list = taskSendService.queryEndorseList(taskNewFilter);
        return coverJqGrid(taskNewFilter, list);
    }
    /**
     * 详情
     *
     * @param id TaskSend表的主键
     * @return
     */
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    public String detail(Model model, @PathVariable String id) {
        TaskSend taskSend = taskSendService.getById(id);
        String taskId = taskSend.getTaskId();
        TaskNew task = taskNewService.getById(taskId);
        List<TaskSend> sendList = taskSendService.queryByTaskId(taskId);
        model.addAttribute("taskNew", task);
        model.addAttribute("sendList", sendList);
        model.addAttribute("taskSendId", id);
        return getView("detail");
    }
    /**
     * 批量签收任务
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/sign/{id}", method = RequestMethod.POST)
    public Message sign(@PathVariable List<String> id) {
        String s = taskLeaderService.signSends(id);
        if ("200".equals(s)) {
            return coverMessage("200");
        }else {
            return coverMessage("500");
        }
    }

    /**
     * 返回批转页面
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "/endorse/{id}", method = RequestMethod.GET)
    public String endorse(Model model, @PathVariable String id) {
        TaskSend taskSend = taskSendService.getById(id);
        String taskId = taskSend.getTaskId();
        TaskNew taskNew = taskNewService.getById(taskId);
        List<TaskSend> sendList = taskSendService.queryByTaskId(taskId);
        model.addAttribute("taskNew", taskNew);
        model.addAttribute("taskSendId", id);
        model.addAttribute("sendList", sendList);
        return getView("form");
    }

    /**
     * 批转任务单位
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/endorse", method = RequestMethod.POST)
    public Message endorse(TaskSendFilter filter) {
        String s = taskLeaderService.endorseTask(filter);
        if ("200".equals(s)) {
            return coverMessage("200");
        } else {
            return coverMessage("500");
        }
    }
}
